package com.meta.platform.sms.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PhoneUtils {
    private final static String reg = "^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(18[0-9]{1})|(19[0-9]{1})|(17[0-9]{1}))+\\d{8})$";
    private final static String hongkong = "^([6|9])\\d{7}$";
    private final static String taiwang = "^[9]\\d{8}$";
    private final static String marcao = "^[6]([8|6])\\d{5}$";
    private final static Map<String, String> map = new HashMap();

    static {
        map.put(hongkong, "852");
        map.put(marcao, "853");
        map.put(taiwang, "886");
    }

    public static String getLocal() {
        return "86";
    }

    public static String getGATRegion(String phone) {
        String[] str = new String[]{hongkong, taiwang, marcao};
        String s = null;
        for (String aa : str) {
            Pattern regex = Pattern.compile(aa);
            Matcher matcher = regex.matcher(phone);
            if (matcher.find()) {
                s = map.get(aa);
                return s;
            }
        }
        return s;
    }


    public static boolean sameRegion(String nativeCode, String phone) {
        if (StringUtils.isEmpty(nativeCode)) {
            return false;
        }
        return "86".equals(nativeCode) ? ifChurchyard(phone) : getGATRegion(phone).equals(nativeCode);
    }


    public static boolean ifChurchyard(String phone) {
        try {
            Pattern regex = Pattern.compile(reg);
            Matcher matcher = regex.matcher(phone);
            return matcher.matches();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean validPhone(String phone) {
        try {
            Pattern regex = Pattern.compile(reg);
            Matcher matcher = regex.matcher(phone);

            Pattern regex_hk = Pattern.compile(hongkong);
            Matcher matcher_hk = regex_hk.matcher(phone);

            Pattern regex_marcao = Pattern.compile(marcao);
            Matcher matcher_marcao = regex_marcao.matcher(phone);

            Pattern regex_tw = Pattern.compile(taiwang);
            Matcher matcher_tw = regex_tw.matcher(phone);

            return matcher.matches() || matcher_hk.matches() || matcher_marcao.matches() || matcher_tw.matches();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    private final static String testReg = "^(((188[8]{4})|(177[7]{4}))+\\d{4})$";
    private final static Pattern testPattern = Pattern.compile(testReg);

    public static boolean isTestPhone(String phone) {
        Matcher matcher = testPattern.matcher(phone);
        return matcher.matches();
    }

    public static void main(String[] args) {
        System.out.println(isTestPhone("18888881234"));
        System.out.println(isTestPhone("18888888234"));
        System.out.println(isTestPhone("18888811234"));

        System.out.println(isTestPhone("17777771234"));
        System.out.println(isTestPhone("17777777234"));
        System.out.println(isTestPhone("17777711234"));
    }

}

